package com.lans.SortAlgorithm;

import java.util.Arrays;

public class ShellSort1 {
    public static void main(String[] args) {
        int[] arr = {12, 34, 54, 2, 3};
        System.out.println("排序前的数组"+Arrays.toString(arr));
        shellSort(arr);
        System.out.println("排序后的数组"+Arrays.toString(arr));
    }
    public static void shellSort(int[] arr){
        int gap = arr.length;
        while(gap>1){
            gap/=2;
            shellSortWithGap(arr,gap);
        }
    }
    public static void shellSortWithGap(int[] arr, int gap){
        for(int i = gap;i<arr.length;i++){
            int temp = arr[i];
            int j;
            for( j = i-gap;j>=0&&arr[j]>temp;j-=gap){
                arr[j+gap] = arr[j];
            }
            arr[j+gap] = temp;
        }
    }
}
